<template>
    <div class="comp-select" :class="{'right-shadow':isOpen===true}">
        <div class="current" @click="toggle($event)">
            <div class="arrow">
                <i class="icon-arrow-down" v-if="isOpen === false"></i>
                <i class="icon-arrow-up" v-if="isOpen === true"></i>
            </div>
            <div class="allow-text fn-ellipsis" v-html="currentLabel"></div>
        </div>
        <transition name="showSel">
            <div class="slide" v-show="isOpen" :class="{'select-shadow':isOpen===true}">
                <ul class="list fn-clearfix">
                    <li class="item" :class="{'item-select':item.val===currentValue}" v-for="item in options">
                        <a href="javascript:;"
                           class="item-link fn-ellipsis"
                           @click="setSelect(item)"
                           :value="item.val"
                           v-html="item.label">
                        </a>
                        <i class="icon-checkmark" v-if="item.val===currentValue"></i>
                    </li>
                </ul>
                <ul class="button-group" v-if="buttons && buttons.length > 0">
                    <li class="item" v-for="item in buttons">
                        <a href="javascript:;"
                           class="item-link ui-btn ui-btn--dashed"
                           v-if="item.show === true"
                           v-html="item.value"
                           @click="doButton(item.callback)"></a>
                    </li>
                </ul>
            </div>
        </transition>
    </div>
</template>

<script type="text/babel">

    /**
     *
     */


    /*
     options: [
         {val: 0, label: 'Cat'},
         {val: 1, label: 'Cow'},
         {val: 2, label: 'Dog'},
         {val: 3, label: 'Elephant'},
         {val: 4, label: 'Fish'},
         {val: 5, label: 'Lion'},
         {val: 6, label: 'Tiger'},
         {val: 7, label: 'Turtle'}
     ],
     selectVal: 5
     */
    export default {
        props: {
            value: {
                default: ''
            },
            options: {
                type: Array,
                default () {
                    return []
                }
            },
            disabled: {
                type: Boolean,
                default: false
            },
            placeholder: {
                type: String,
                default: null
            },
            onChange: {
                type: Function,
                default() {}
            },
            buttons: {
                type: Array,
                default () {
                    return []
                }
            }
        },
        computed: {
            currentLabel() {
                if (this.options.length > 0) {
                    let current = _.find(this.options, {
                        val: this.currentValue
                    });
                    if (current) {
                        return current.label;
                    } else if (this.placeholder) {
                        return this.placeholder;
                    } else {
                        // 默认选择第一项
                        return this.options[0].label;
                    }
                } else {
                    return -1;
                }
            }
        },
        watch:{
            value(newVal,oldVal){
                let param = {val:newVal};
                this.setSelect(param);
            }
        },
        data() {
            return {
                isOpen: false,
                currentValue: this.value
            };
        },
        created() {

        },
        mounted() {
            $(document).on('click', (e) => {
                if (this.isOpen === true) {
                    this.hide();
                }
            });
        },
        methods: {
            setSelect(item) {
                this.currentValue = item.val;
                this.hide();
                this.selectVal();//通知父级
            },
            toggle($event) {
                $event.stopPropagation();
                if (this.isOpen === true) {
                    this.hide();
                } else {
                    this.open();
                }
            },
            open() {
                this.isOpen = true;
            },
            hide() {
                this.isOpen = false;
            },
            selectVal(){
                this.$emit('selectVal', this.currentValue);
            },
            doButton(callback) {
                this.hide();
                callback();
            },
        },
        beforeDestroy() {
            $(document).off();
        }
    }
</script>

<style lang="sass" rel="stylesheet/scss">
    .comp-select {
        $color-font: #333;
        $color-border: #ccc;
        $color-border-hover: #a9a9a9;
        $compHeight: 32px;

        position: relative;
        display: inline-block;
        width: 255px;
        background-color: #fff;

        &--open {
            border-bottom: 0 none;
        }
        .icon {
            margin-right: 8px;
            font-size: 18px;
            vertical-align: middle;
        }
        .current {
            box-sizing: border-box;
            height: $compHeight;
            line-height: $compHeight;
            border-radius: 4px;
            border: 1px $color-border solid;
            padding: 0 8px;
            cursor: default;
            &:hover {
                border: 1px $color-border-hover solid;
            }
            .arrow {
                float: right;
            }
            .allow-text{
              //  vertical-align: middle;
            }
        }
        .showSel-enter-active, .showSel-leave-active {
            opacity: 1;
        }
        .showSel-enter, .showSel-leave-active {
            opacity: 0 ;
        }
        .slide {
            overflow: hidden;
            position: absolute;
            top: 30px;
            left: 0;
            z-index: 1;
            border-top: 1px $color-border solid;
            border-left: 1px $color-border solid;
            border-right: 1px $color-border solid;
            border-bottom: 1px $color-border solid;
            width: 100%;
            box-sizing: border-box;
            border-radius: 2px;
            background-color: #fff;
            .list {
                padding: 8px;
                background-color: #fff;
                .item {
                    position: relative;
                    &:first-child {

                    }
                    .icon-checkmark{
                        position: absolute;
                        right: 8px;
                        top:12px;
                        font-size: 12px;
                        color:#0b95ff;
                    }
                    .item-link {
                        display: block;
                        padding: 0 24px 0 6px;
                        height: 32px;
                        line-height: 32px;
                        color: $color-font;
                        &:hover {
                            background-color: #fafafb;
                        }
                    }
                    &.selected {
                        .item-link {
                            background-color: #fafafb;
                        }
                    }
                }
                .item-select{
                    background-color: #fafafb;
                }
            }
            .button-group {
                .item {
                    border-top: 1px $color-border solid;
                    padding: 8px;
                    .item-link {
                        display: block;
                        transition: color 0s;
                        &:hover {
                            background-color: #fafafb;
                            border: 1px #0b95ff dashed;
                            color: #0b95ff;
                        }
                    }

                }
            }
        }

    }
    .select-shadow{
        box-shadow: 0 0 4px 2px #fafafb;
    }
    .right-shadow{
        box-shadow: 0 0 4px 2px #fafafb;
    }

</style>